﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.General.Popup;
using JBeamLibrary.VehicleParts;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.Popups
{
    public partial class NodeSelectionPopup : PopupContainer
    {
        #region Fields

        private VehiclePart _vehiclePart;

        public event EventHandler VehiclePartChanged;
        public event EventHandler<Node> SelectedNodeChanged;

        #endregion

        #region Properties

        public VehiclePart VehiclePart
        {
            get { return _vehiclePart; }
            set
            {
                if (_vehiclePart == value) return;
                _vehiclePart = value;
                OnVehiclePartChanged();
            }
        }

        public Node SelectedNode
        {
            get { return nodesControl.SelectedNode; }
            set { nodesControl.SelectedNode = value; }
        }

        #endregion

        #region CTORs

        public NodeSelectionPopup()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void xbClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void xbReload_Click(object sender, EventArgs e)
        {
            nodesControl.Reload();
        }

        private void nodesControl_NodeValidated(object sender, Node e)
        {
            OnSelectedNodeChanged();
            Close();
        }

        private void nodesControl_SelectedNodeChanged(object sender, Node e)
        {
            OnSelectedNodeChanged();
        }

        #endregion

        #region Methods

        protected virtual void OnSelectedNodeChanged()
        {
            if (SelectedNodeChanged != null)
                SelectedNodeChanged(this, SelectedNode);
        }

        private void OnVehiclePartChanged()
        {
            nodesControl.VehiclePart = VehiclePart;

            if (VehiclePartChanged != null)
                VehiclePartChanged(this, new EventArgs());
        }

        #endregion
    }
}
